"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function getCurrentPages() {
	return [{ route: 'currentPage' }];
}
var Stores = /** @class */ (function () {
	function Stores(parameters) {
		this._store = new Map();
		this.listenerList = new Map();
		this.store = parameters;
	}
	Object.defineProperty(Stores.prototype, "store", {
		get: function () {
			return this._store;
		},
		set: function (obj) {
			this.set(obj);
		},
		enumerable: false,
		configurable: true
	});
 	Stores.prototype.get = function (key) {
		return this._store.get(key);
	};
	Stores.prototype.has = function (key) {
		return this._store.has(key);
	};
	Stores.prototype.delete = function (key) {
		return this._store.delete(key);
	};
	Stores.prototype.set = function (obj, val) {
		if (typeof obj === "string" && val !== undefined) {
			this.triggerListener("set", obj, this._store.get(obj));
			this._store.set(obj, val);
		}
		else if (typeof obj === "object") {
			for (var key in obj) {
				this.triggerListener(key, obj[key], this._store.get(key));
				this._store.set(key, obj[key]);
			}
		}
	};
	Stores.prototype.on = function (key, listener) {
		var listenerConfig = {
			handler: Function,
			executed: false,
			pageUrl: this.getPageUrl()
		};
		if (typeof listener === "function") {
			listenerConfig.handler = listener;
		}
		else {
			listenerConfig = listener;
		}
		this.setListener(key, listenerConfig);
	};
	Stores.prototype.setListener = function (key, listener) {
		if (this.hasListener(key)) {
			this.listenerList.get(key).push(listener);
		}
		else {
			this.listenerList.set(key, [listener]);
		}
	};
	Stores.prototype.hasListener = function (key) {
		return this.listenerList.has(key);
	};
	Stores.prototype.getCurrPage = function () {
		var pageList = getCurrentPages();
		return pageList[pageList.length - 1] || { route: '' };
	};
	Stores.prototype.getPageUrl = function () {
		return this.getCurrPage().route;
	};
	Stores.prototype.triggerListener = function (key, newVal, oldVal) {
		var currPageUrl = this.getPageUrl();
		if (this.hasListener(key)) {
			this.listenerList.get(key).filter(function (x) {
				var isFilter = false;
				if (!x.inPage) {
					isFilter = true;
				}
				if (x.inPage && currPageUrl === x.pageUrl) {
					isFilter = true;
				}
				if (x.once && x.executed) {
					isFilter = false;
				}
				return isFilter;
			}).forEach(function (x) {
				x.executed = true;
				x.handler(newVal, oldVal);
			});
		}
	};
	Stores.prototype.createProxy = function (store) {
		for (var item in store) {
			if (typeof store[item] === 'object') {
				store[item] = this.proxyStore(store[item]);
			}
		}
		return this.proxyStore(store);
	};
	Stores.prototype.proxyStore = function (store) {
		return new Proxy(store, {
			get: function (target, prop) {
				return Reflect.get(target, prop);
			},
			set: function (target, prop, value) {
				console.warn('Warning:请使用set方法操作store');
				return true;
			}
		});
	};
	return Stores;
}());
exports.default = Stores;
